﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Spine.Unity;

public class SpineDragger : MonoBehaviour
{
    public bool isDragging = false;
    Vector3 hitPos;
    Vector3 offset;
    Vector3 worldStartDragPos;
    SkeletonAnimation skeletonAnimation;

    private void Start()
    {
        skeletonAnimation = GetComponent<SkeletonAnimation>();
    }

    private void FixedUpdate()
    {
        if (!Utils.IsOnUI(AppMgr.Instance.mainUI.graphicRaycaster, Input.mousePosition))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.gameObject == gameObject)
                {
                    if (Input.GetMouseButtonDown(0))
                    {
                        worldStartDragPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
                        isDragging = true;
                        offset = transform.position - worldStartDragPos;
                    }

                    if (Input.GetMouseButtonUp(0))
                    {
                        isDragging = false;
                        SpineCreateControl.Instance.UpdateCurrentSpineObject(gameObject);
                    }
                }
            }

            if (isDragging)
            {
                worldStartDragPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
                transform.position = worldStartDragPos + offset;
            }
        }
    }
}
